﻿Imports System.Text.RegularExpressions

Public Class 商品信息查询

    Private database As DataAccess
    Private data_Good As DataTable

    Private Sub 商品信息查询_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        database = New DataAccess
        database.OpenConnection()

        Dim sqlString As String
        sqlString = "select * from tf_f_goods"
        data_Good = database.QueryDatabase(sqlString)

        '初始化商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

        '读取当前列表中商品的信息
        get_good_info()

    End Sub
    Private Sub GoodTypeList_Init()

        ComboBox_Good_Type.Items.Clear()

        '将商品类型数据列在下拉菜单中
        Dim row As DataRow
        For Each row In data_Good.Rows
            'MessageBox.Show("data_Good.Rows1")
            Dim good_type As String
            good_type = row("goods_type")

            ' 去除重复项
            If ComboBox_Good_Type.FindString(good_type) = -1 Then
                ComboBox_Good_Type.Items.Add(good_type)
            End If

        Next row

        If ComboBox_Good_Type.Items.Count > 0 Then
            ComboBox_Good_Type.SelectedIndex = 0
        End If

    End Sub

    Private Sub GoodNameList_Init()

        ComboBox_Good_Name.Items.Clear()

        '从数据库中获取商品名称信息
        Dim good_type As String = ComboBox_Good_Type.Text
        'Dim sqlString As String
        'sqlString = "select goods_name from tf_f_goods where goods_type ='" & good_type & "'"
        'data_Good = database.QueryDatabase(sqlString)

        Dim rows() As DataRow
        rows = data_Good.Select("goods_type ='" & good_type & "'")

        '将商品名称数据列在下拉菜单中
        Dim row As DataRow
        For Each row In rows
            'MessageBox.Show("data_Good.Rows2")
            ComboBox_Good_Name.Items.Add(row("goods_name"))
        Next row

        If ComboBox_Good_Name.Items.Count > 0 Then
            ComboBox_Good_Name.SelectedIndex = 0
        End If

    End Sub

    Private Sub get_good_info()

        Dim good_id As String = ""
        Dim good_type As String = ""
        Dim good_name As String = ""
        Dim wholesale_price As String = ""
        Dim retail_price As String = ""
        Dim num As String = ""
        Dim unit As String = ""
        Dim remark As String = ""

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text

        '检查输入是否合法
        If check_input_string(good_type) = False Then
            MessageBox.Show("商品类型非法")
            Return
        End If
        If check_input_string(good_name) = False Then
            MessageBox.Show("商品名称非法")
            Return
        End If

        '读取商品信息
        Dim rows() = data_Good.Select("goods_type='" & good_type & "' and goods_name='" & good_name & "'")
        If rows.Length = 1 Then
            Dim row As DataRow
            For Each row In rows
                good_id = row.Item("goods_id")
                wholesale_price = row.Item("wholesale_price")
                retail_price = row.Item("retail_price")
                num = row.Item("num")
                unit = row.Item("unit")
                remark = row.Item("remark")
            Next row
        ElseIf rows.Length > 1 Then
            MessageBox.Show("在数据库中查询商品是出现错误")
        End If

        '显示商品信息
        TextBox_Batch_Price.Text = wholesale_price
        TextBox_Retail_Price.Text = retail_price
        TextBox_Total_Number.Text = num
        TextBox_Good_Unit.Text = unit
        TextBox_Good_Remark.Text = remark

    End Sub

    Private Sub ComboBox_Good_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Type.SelectedIndexChanged

        '更新商品名称列表
        GoodNameList_Init()

        '读取商品信息
        get_good_info()

        '关闭添加按钮
        Button_Good_Add.Enabled = False

        '使能删除按钮
        Button_Good_Delele.Enabled = True

        '关闭更新按钮
        Button_Good_Update.Enabled = False

    End Sub

    Private Sub ComboBox_Good_Type_TextUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Type.TextUpdate

        '清空商品信息
        TextBox_Batch_Price.Text = ""
        TextBox_Retail_Price.Text = ""
        TextBox_Total_Number.Text = ""
        TextBox_Good_Unit.Text = ""
        TextBox_Good_Remark.Text = ""

        '使能添加按钮
        Button_Good_Add.Enabled = True

        '关闭删除按钮
        Button_Good_Delele.Enabled = False

        ''关闭更新按钮
        'Button_Good_Update.Enabled = False

    End Sub

    Private Sub ComboBox_Good_Name_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Name.SelectedIndexChanged

        '读取商品信息
        get_good_info()

        '关闭添加按钮
        Button_Good_Add.Enabled = False

        '使能删除按钮
        Button_Good_Delele.Enabled = True

        '关闭更新按钮
        Button_Good_Update.Enabled = False

    End Sub


    Private Sub ComboBox_Good_Name_TextUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Name.TextUpdate

        '清空商品信息
        TextBox_Batch_Price.Text = ""
        TextBox_Retail_Price.Text = ""
        TextBox_Total_Number.Text = ""
        TextBox_Good_Unit.Text = ""
        TextBox_Good_Remark.Text = ""

        '使能添加按钮
        Button_Good_Add.Enabled = True

        '关闭删除按钮
        Button_Good_Delele.Enabled = False

        '关闭更新按钮
        Button_Good_Update.Enabled = False

    End Sub

    Private Sub Button_Good_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Good_Add.Click

        Dim good_type As String
        Dim good_name As String

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text

        '检查输入是否合法
        If check_input_string(good_type) = False Then
            MessageBox.Show("商品类型非法")
            Return
        End If
        If check_input_string(good_name) = False Then
            MessageBox.Show("商品名称非法")
            Return
        End If

        '判断数据库中是否已有此商品
        If get_good_id(good_type, good_name) > 0 Then
            MessageBox.Show("当前数据库中已存在此商品")
            Return
        End If

        '获取当前数据库中最大商品编号
        Dim max_good_id As Integer
        max_good_id = get_max_good_id()

        '将商品信息存入数据库中
        'TODO: 添加更详细的商品信息
        '      批发价, 零售价, 库存数量, 计量单位, 备注
        If add_good_process(max_good_id + 1, good_type, good_name) = False Then
            MessageBox.Show("添加商品信息失败")
            Return
        Else
            MessageBox.Show("添加商品信息成功")
        End If

        '刷新下拉菜单数据
        GoodTypeList_Init()
        GoodNameList_Init()

    End Sub

    Private Function check_input_string(ByVal str As String) As Boolean

        '    是否为空
        '    是否有非法字符

        If Regex.Match(str, "( )+").Success = True Then
            Return False
        End If

        If str = "" Then
            Return False
        End If

        Return True
    End Function

    Private Function get_max_good_id() As Integer

        Dim max_good_id As Integer = 0

        Dim row As DataRow
        For Each row In data_Good.Rows
            If max_good_id <= row.Item("goods_id") Then
                max_good_id = row.Item("goods_id")
            End If
        Next row

        Return max_good_id
    End Function

    Private Function get_good_id(ByVal good_type As String, ByVal good_name As String) As Integer

        Dim rows() = data_Good.Select("goods_type='" & good_type & "' and goods_name='" & good_name & "'")

        If rows.Length = 1 Then
            Dim row As DataRow
            For Each row In rows
                Return row.Item("goods_id")
            Next row
        ElseIf rows.Length > 1 Then
            MessageBox.Show("在数据库中查询商品是出现错误")
            Return -1
        End If

        Return 0
    End Function

    Private Function add_good_process(ByVal good_id As Integer, ByVal good_type As String, ByVal good_name As String) As Boolean

        database.SetSelectCommand("select * from tf_f_goods")

        Dim row As DataRow
        row = data_Good.NewRow
        data_Good.Rows.Add(row)

        row("goods_id") = good_id
        row("goods_type") = good_type
        row("goods_name") = good_name
        row("wholesale_price") = 0
        row("retail_price") = 0
        row("num") = 0
        row("unit") = "无"
        row("remark") = ""

        Return database.UpdateDatabase(data_Good)

    End Function

    Private Sub TextBox_Batch_Price_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Batch_Price.TextChanged

        '使能更新按钮
        Button_Good_Update.Enabled = True

    End Sub

    Private Sub TextBox_Retail_Price_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Retail_Price.TextChanged

        '使能更新按钮
        Button_Good_Update.Enabled = True

    End Sub

    Private Sub TextBox_Total_Number_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Total_Number.TextChanged

        '使能更新按钮
        Button_Good_Update.Enabled = True

    End Sub

    Private Sub TextBox_Good_Unit_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Good_Unit.TextChanged

        '使能更新按钮
        Button_Good_Update.Enabled = True

    End Sub

    Private Sub TextBox_Good_Remark_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Good_Remark.TextChanged

        '使能更新按钮
        Button_Good_Update.Enabled = True

    End Sub

    Private Sub Button_Good_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Good_Update.Click

        '更新商品信息实体表
        If update_good_info() = True Then
            MessageBox.Show("更新商品信息成功")
        Else
            'MessageBox.Show("更新商品信息失败")
        End If

        '更新商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

    End Sub

    Private Function update_good_info() As Boolean

        '读取商品数据信息
        Dim good_id As String = ""
        Dim good_type As String = ""
        Dim good_name As String = ""
        Dim wholesale_price As String = ""
        Dim retail_price As String = ""
        Dim num As String = ""
        Dim unit As String = ""
        Dim remark As String = ""

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text
        wholesale_price = TextBox_Batch_Price.Text
        retail_price = TextBox_Retail_Price.Text
        num = TextBox_Total_Number.Text
        unit = TextBox_Good_Unit.Text
        remark = TextBox_Good_Remark.Text

        'TODO:
        '  检查输入的数据是否合法

        '检查数据库中是否有此商品
        good_id = get_good_id(good_type, good_name)
        If good_id = 0 Then
            MessageBox.Show("数据库中没有此商品信息")
            Return False
        End If

        '更新商品实体表
        database.SetSelectCommand("select * from tf_f_goods")
        Dim row As DataRow

        For Each row In data_Good.Rows
            If good_id = row.Item("goods_id") Then
                row("goods_id") = good_id
                row("goods_type") = good_type
                row("goods_name") = good_name
                row("wholesale_price") = wholesale_price
                row("retail_price") = retail_price
                row("num") = num
                row("unit") = unit
                row("remark") = remark
            End If
        Next row

        If database.UpdateDatabase(data_Good) = False Then
            MessageBox.Show("更新商品数据库时失败")
            Return False
        End If

        Return True
    End Function

    Private Sub Button_Good_Delele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Good_Delele.Click

        If delete_good() = True Then
            MessageBox.Show("删除商品成功")
        Else
            MessageBox.Show("删除商品失败")
        End If

        '更新商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

    End Sub

    Private Function delete_good() As Boolean

        Dim good_type As String = ""
        Dim good_name As String = ""

        good_type = ComboBox_Good_Type.Text
        good_name = ComboBox_Good_Name.Text

        '检查数据库中是否有此商品
        Dim good_id = get_good_id(good_type, good_name)
        If good_id = 0 Then
            MessageBox.Show("数据库中没有此商品信息")
            Return False
        End If

        '删除商品
        database.SetSelectCommand("select * from tf_f_goods")
        Dim row As DataRow

        For Each row In data_Good.Rows
            If good_id = row.Item("goods_id") Then
                row.Delete()
            End If
        Next

        If database.UpdateDatabase(data_Good) = False Then
            MessageBox.Show("更新商品数据库时失败")
            Return False
        End If

        Return True
    End Function

End Class